<%@ page language="java" pageEncoding="UTF-8"%>
<%@ page import="java.util.List"%>
<%@ page import="edu.ntu.im.p2ptrade.entity.*"%>
<% List<Item> items = (List<Item>)request.getAttribute("items");%>
<html>
<head>
<title>User Items</title>
<script type="text/javascript" src="/p2ptrade/js/json.js"></script>
<script type="text/javascript" src="/p2ptrade/js/ajax.js"></script>
<script>
function setdata(name,memo,url,sID,del){
	document.form_item.name.value= name;
	document.form_item.memo.value= memo;
	document.form_item.url.value= url;
	document.form_item.id.value= sID;
	document.form_item.del.value= del;
}

function prepareToUpdate(){
  var req = createAjaxRequest2(function(result){
	if (result.success){
	   alert("Obtain lock successful!");
	}else {
	   alert("Fail to obtain lock!");
	}
   });
  req.open("GET", "/p2ptrade/prepareToUpdate.do");
  req.send(null);
}

</script>
<link href="../css/css.css" rel="stylesheet" type="text/css">
</head>
<body>
<form name="form_item" method="post">
	<div align="center">
	
	  <p>
	    <jsp:include page="/top.tpl" />      
	    
      </p>
	  <table width="500" class="TABLE">
	<tr>
		<td colspan="4" align="center" class="header">My Items
          <input type="hidden" name="id" value="">
          <input type="hidden" name="del" value="">
		</td>
	</tr>
	<tr>
		<td colspan="1" class="content_font">item name</td>
		<td colspan="1"><input type="text" name="name" class="text_font"></td>
	</tr>
	<tr>
		<td colspan="1" class="content_font">memo</td>
		<td colspan="1"><textarea name="memo" col="5" row="3" class="text_font"></textarea></td>
	</tr>
	<tr>
		<td colspan="1" class="content_font">photo</td>
		<td colspan="1" class="content_font">http://<input type="text" name="url" class="text_font"></td>
	</tr>
	<tr><td></td></tr>
	</table>
	
	<div align="center">
		<p>
		  <input name="send" type="button" class="text_font" onClick="document.forms[0].action = '/p2ptrade/item/addOrUpdateItem.do';document.forms[0].submit();" value="add">
		  <input name="update" type="button" class="text_font" onClick="document.forms[0].action = '/p2ptrade/item/addOrUpdateItem.do';document.forms[0].submit();" value="update">
	  </p>
		<p>&nbsp;</p>
	  </div>
	<table width="500" border="0" cellpadding="0" cellspacing="0">
	<tr>
		<td colspan="4" align="center" class="header">Item List</td>
	</tr>
	<%
	int i = 0 ;
	for (Item item:items) {
	String td_style = "";
	if ( i%2 == 0 ) {td_style="td_bk1";} else {td_style="td_bk2";} ; 
	i++; %>
	<tr class="<%=td_style %>">
		<td colspan="1" style="padding:5px;">item name</td>
		<td colspan="1" style="padding:5px;"><%=item.getName() %></td>
		<td colspan="1" style="padding:5px;" align=right>
			<input type="button" class="text_font" id="edit<%=item.getId() %>" onClick="setdata('<%=item.getName() %>','<%=item.getMemo() %>','<%=item.getPhotoUrl() %>','<%=item.getId() %>', '');prepareToUpdate();" value="edit">
			<input type="button" class="text_font" id="delete<%=item.getId() %>" onClick="setdata('<%=item.getName() %>','<%=item.getMemo() %>','<%=item.getPhotoUrl() %>','<%=item.getId() %>', 'T');document.forms[0].action = '/p2ptrade/item/addOrUpdateItem.do';document.forms[0].submit();" value="delete">
		</td>
	</tr>
	<tr class="<%=td_style %>">
		<td colspan="1" style="padding:5px;">memo</td>
		<td colspan="2" style="padding:5px;"><%=item.getMemo()%></td>
	</tr>
	<tr class="<%=td_style %>">
		<td colspan="1" style="padding:5px;">photo</td>
		<td colspan="2" style="padding:5px;"><img src="http://<%=item.getPhotoUrl() %>" style="width:100px;height:100px;border:1px solid" ></img></td>
	</tr>
	
	<%} %>
	</table>
  	  
  </div>
	
</form>


</body>
</html>